package com.sneakxy.actions.code;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 类方法
 * @author 潜行的青衣
 * @see
 */
public class ClassMethod {

	private String name;
	
	private String comment;
	
	private boolean construction;
	
	private boolean interfaceMethod;
	
	private Permission permission = Permission.PUBLIC;
	
	private List<Argument> arguments = new ArrayList<>();
	
	private ClassType returnType;
	
	private List<String> lines = new ArrayList<>();
	
	private List<Annotation> annotations = new ArrayList<>();
	
	public ClassMethod(String name) {
		this.name = name;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public boolean isConstruction() {
		return construction;
	}

	public void setConstruction(boolean construction) {
		this.construction = construction;
	}

	public Argument addArgument(Argument arg) {
		this.arguments.add(arg);
		return arg;
	}
	
	public Annotation addAnnotation(Annotation annotation) {
		this.annotations.add(annotation);
		return annotation;
	}
	
	public void addCodeLine(String code) {
		this.lines.add(code);
	}
	
	public void setReturnType(ClassType returnType) {
		this.returnType = returnType;
	}
	
	public boolean isInterfaceMethod() {
		return interfaceMethod;
	}

	public void setInterfaceMethod(boolean interfaceMethod) {
		this.interfaceMethod = interfaceMethod;
	}

	public List<Type> getImportClassType() {
		List<Type> result = new ArrayList<>();
		Iterator<Argument> it = this.arguments.iterator();
		while(it.hasNext()) {
			result.addAll(it.next().getImportClassType());
		}
		result.addAll(this.annotations);
		if(this.returnType != null) {
			result.add(returnType);
		}
		return result;
	}
	
	public String toMethodString() {
		StringBuilder s = new StringBuilder();
		//注释
		if(StringUtils.isNoneBlank(this.comment)) {
			s.append("\t/**\r\n");
			s.append("\t * ").append(comment).append("\r\n");
			s.append("\t */\r\n");
		}
		//注解
		Iterator<Annotation> annit = annotations.iterator();
		while(annit.hasNext()) {
			s.append("\t").append(annit.next().toString(true)).append("\r\n");
		}
		//方法
		s.append("\t")
			.append(this.permission.value());
		if(!this.isConstruction()) {
			s.append(" ").append(this.returnType != null ? this.returnType.toString(true) : "void");
		}
		s.append(" ").append(this.name).append("(");
		Iterator<Argument> it = arguments.iterator();
		Argument arg = null;
		while(it.hasNext()) {
			arg = it.next();
			s.append(arg.toArgumentString());
			if(it.hasNext()) {
				s.append(", ");
			}
		}
		if(isInterfaceMethod()) {
			s.append(");\r\n");
		} else {
			s.append(") {\r\n");
			//命令行
			Iterator<String> lineit = this.lines.iterator();
			while(lineit.hasNext()) {
				s.append("\t\t").append(lineit.next()).append("\r\n");
			}
			s.append("\t}\r\n");
		}
		return s.toString();
	}
	
}
